package com.lyw.home

import androidx.activity.viewModels
import androidx.fragment.app.viewModels
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.lu.library.base.BaseActivity
import com.lyw.home.databinding.ActivityPoetyDetailBinding
import com.lyw.home.databinding.ActivityQuestionSelectionBinding
import dagger.hilt.android.AndroidEntryPoint
import me.shouheng.utils.ktx.onDebouncedClick

/**
 * Author: luyuanwei
 * Date: 2025/1/22/周三
 * Description: 选择题类型
 * Version:
 * Copyright(C):2024-2025
 */
@Route(path = HomeRouterPaths.HOME_QUESTION_SELECTION)
@AndroidEntryPoint
class QuestionSelectionActivity: BaseActivity<ActivityQuestionSelectionBinding>() {
    private val questionVm by viewModels<QuestionVm>()
    private lateinit var questionAdapter: QuestionAdapter

    @Autowired(name="KnowledgeType")
    @JvmField
     var knowledgeType:ContentItem = ContentItem("","","","",0)

    override fun initListener() {
        super.initListener()

    }

    override fun initData() {
        binding.setVariable(BR.vm, questionVm)
        setTitle(knowledgeType.topType+"/"+knowledgeType.name)
        questionVm.initData(knowledgeType.topType,knowledgeType.subType,knowledgeType.typeIndex);
        setupViewPager()
    }

    private fun setupViewPager() {
        questionAdapter = QuestionAdapter(questionVm.questions.value ?: emptyList(), questionVm)
        binding.viewPager.adapter = questionAdapter
        binding.viewPager.orientation = ViewPager2.ORIENTATION_HORIZONTAL

        questionVm.questions.observe(this) { questions ->
            questionAdapter.addAll(questions)
//            homeVm.currentQuestion.value = questions.getOrNull(homeVm.index.value!!)
        }
        questionVm.itemChanged.observe(this){
            questionAdapter.notifyItemChanged(it)
        }
//        homeVm.index.observe(viewLifecycleOwner) { index ->
//            binding.viewPager.currentItem = index
//        }

        binding.viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
//                homeVm._index.value = position
//                homeVm._currentQuestion.value = homeVm.poetries.value?.getOrNull(position)
            }
        })
    }

    override fun layoutResId(): Int =R.layout.activity_question_selection
}